/*
 * Copyright 2010-2014 icl-network.com. All rights reserved.
 * Support: http://www.icl-network.com
 */
package com.wolfheader.wolfgame.controller;

import java.util.Date;
import java.util.Iterator;
import java.util.Set;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.wolfheader.wolfgame.DateEditor;
import com.wolfheader.wolfgame.Message;
import com.wolfheader.wolfgame.util.SpringUtils;

/**
 * Controller - 基类
 * 
 * @author ICLNetwork Team
 * @version 3.0
 */
public class BaseController {

	/** 主页重定向URL */
	protected static final String HOMEPAGE_REDIRECT_URL = "redirect:/";

	/** 错误视图 */
	protected static final String ERROR_VIEW = "/error";

	/** 错误消息 */
	protected static final Message ERROR_MESSAGE = Message.error("操作错误");

	/** 成功消息 */
	protected static final Message SUCCESS_MESSAGE = Message.success("操作成功");

	public static final String FLASH_MESSAGE_ATTRIBUTE_NAME = "flash_message";

	@Resource(name = "validator")
	private Validator validator;

	/**
	 * 数据绑定
	 * 
	 * @param binder
	 *            WebDataBinder
	 */
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
		binder.registerCustomEditor(Date.class, new DateEditor(true));
	}

	/**
	 * 数据验证
	 * 
	 * @param target
	 *            验证对象
	 * @param groups
	 *            验证组
	 * @return 验证结果
	 */
	protected boolean verify(Object target, Class<?>... groups) {
		return validator.validate(target, groups).isEmpty();
	}

	/**
	 * 数据验证
	 * 
	 * @param redirectAttributes
	 *            重定向参数
	 * @param target
	 *            验证对象
	 * @param groups
	 *            验证组
	 * @return 验证结果
	 */
	protected boolean verify(RedirectAttributes redirectAttributes, Object target, Class<?>... groups) {
		Set<ConstraintViolation<Object>> constraintViolations = validator.validate(target, groups);
		if (constraintViolations.isEmpty()) {
			return true;
		} else {
			StringBuffer message = new StringBuffer("");
			for (Iterator<ConstraintViolation<Object>> iterator = constraintViolations.iterator(); iterator
					.hasNext();) {
				ConstraintViolation<Object> constraintViolation = (ConstraintViolation<Object>) iterator.next();
				message.append("<p>");
				message.append(constraintViolation.getMessage());
				message.append("</p>");
			}
			return false;
		}
	}

	/**
	 * 数据验证
	 * 
	 * @param type
	 *            类型
	 * @param property
	 *            属性
	 * @param value
	 *            值
	 * @param groups
	 *            验证组
	 * @return 验证结果
	 */
	protected boolean verify(Class<?> type, String property, Object value, Class<?>... groups) {
		return validator.validateValue(type, property, value, groups).isEmpty();
	}

	/**
	 * 数据验证
	 * 
	 * @param redirectAttributes
	 *            重定向参数
	 * @param type
	 *            类型
	 * @param property
	 *            属性
	 * @param value
	 *            值
	 * @param groups
	 *            验证组
	 * @return 验证结果
	 */
	protected boolean verify(RedirectAttributes redirectAttributes, Class<?> type, String property, Object value,
			Class<?>... groups) {
		Set<?> constraintViolations = validator.validateValue(type, property, value, groups);
		if (constraintViolations.isEmpty()) {
			return true;
		} else {
			StringBuffer message = new StringBuffer("");
			for (Iterator<?> iterator = constraintViolations.iterator(); iterator.hasNext();) {
				ConstraintViolation<?> constraintViolation = (ConstraintViolation<?>) iterator.next();
				message.append("<p>");
				message.append(constraintViolation.getMessage());
				message.append("</p>");
			}
			return false;
		}
	}

	/**
	 * 获取国际化消息
	 * 
	 * @param code
	 *            代码
	 * @param args
	 *            参数
	 * @return 国际化消息
	 */
	protected String message(String code, Object... args) {
		return SpringUtils.getMessage(code, args);
	}

	/**
	 * 添加瞬时消息
	 * 
	 * @param redirectAttributes
	 *            RedirectAttributes
	 * @param message
	 *            消息
	 */
	protected void addFlashMessage(RedirectAttributes redirectAttributes, Message message) {
		if (redirectAttributes != null && message != null) {
			redirectAttributes.addFlashAttribute("content", message.getCont());
			redirectAttributes.addFlashAttribute("type", message.getType());
		}
	}
}